<template>
  <div class="article-card" @click="detailArticle">
    <section class="article-info">
      <div class="info-head">
        <error-img class="article-img" :src="article?.preview" />
        <div class="article-user">
          <error-img class="head-img" :src="article?.user?.imgUrl" fit="cover" is-login />
          <div class="user-name">{{ article?.user?.name }}</div>
        </div>
      </div>
      <div class="info-desc">
        <p class="info-title">
          {{ article?.title }}
        </p>
        <p class="info-comment">{{ article?.description ?? '没有描述...' }}</p>
      </div>
    </section>
  </div>
</template>

<script setup lang="ts">
import type { ArticleApiType } from '@/apis/article/namespace';
import { RouterEnum } from '@/enum';

const props = defineProps({
  width: String,
  article: {
    type: Object as PropType<ArticleApiType.Article>,
    required: true,
  },
});
const router = useRouter();

const detailArticle = () => {
  const routePath = router.resolve({
    name: RouterEnum.HOME_ARTICLE_DETAIL,
    query: { articleId: props.article.id },
  }).href;
  window.open(routePath, '_blank');
};
</script>

<style scoped lang="scss">
.article-card {
  width: 16rem;
  height: fit-content;
  background: #000;
  border: 1px solid transparent;
  border-radius: 2rem 2rem 0;
  &:active {
    background: linear-gradient(45deg, #d7d000, #75b801);
    animation: bg-pulse 3s infinite;
  }
  @keyframes bg-pulse {
    0% {
      transform: translateX(var(--w)) translateY(var(--h)) scale(1);
      background: #d7d000;
    }
    25% {
      background: #a5d700;
    }
    50% {
      background: #75b801;
    }
    75% {
      background: #a5d700;
    }
    100% {
      background: #d7d000;
    }
  }
}
.article-info {
  --bg: #222;
  margin: 4px;
  border-radius: 1.8rem 1.8rem 0;
  background-color: var(--bg);
  cursor: pointer;
  overflow: hidden;
  .article-img {
    min-width: 12rem;
    min-height: 8rem;
  }
  .info-head {
    position: relative;
    .article-user {
      position: absolute;
      bottom: -1.6rem;
      left: 0.5rem;
      display: flex;
      align-items: end;
      width: 100%;
      .head-img {
        width: 4rem;
        height: 4rem;
        border: 2px solid var(--bg);
        border-radius: 50%;
      }
      .user-name {
        flex: 1;
        margin: 0 1rem 2px 0.5rem;
        color: #5a5a5a;
        font-weight: bold;
        border-bottom: 2px solid #5a5a5a;
      }
    }
  }
  .info-desc {
    margin: 2rem 1rem 0.5rem;
    font-weight: 600;
    .info-title {
      color: #fff;
      font-size: 14px;
    }
    .info-comment {
      font-size: 12px;
      color: #ababa9;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }
}
</style>
